#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<cassert>
#include<iostream>
#include<stdio.h>
#include<string>

using std::string;
using std::cout;
using std::endl;

const char* pathname = ".";
const int proj_id = 1;
const int size = 1024;

int getKey()
{
    return ftok(pathname,proj_id);
}

// char*  toHex(int key)
string toHex(int key) //使用char* 会导致返回已释放空间指针错误，而返回string则会调用拷贝构造构造临时对象，默认深拷贝将内容拷贝下来
{
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"%#x",key);
    return buffer;
}

int creatShmhelp(int key,int size,int flag)
{
    umask(0);
    return shmget(key, size, flag);
}

int creatShm(int key,int size) // 两个标志位共同使用表示存在则报错
{   
    return creatShmhelp(key,size,IPC_CREAT|IPC_EXCL|0666);
}

int getShm(int key,int size)
{
    return creatShmhelp(key,size, IPC_CREAT);
}

int delShm(int shmid)
{
    int ret = shmctl(shmid,IPC_RMID,nullptr);
    assert(ret!=-1);
}
